package EvaTCPL;

public class EvaTCPLif extends EvaTCPLaexpression
{
  EvaTCPLbexpression condition;
  EvaTCPLaexpression thenpart;
  EvaTCPLelseif elseifpart;
  EvaTCPLaexpression elsepart;

  public EvaTCPLif(EvaTCPLbexpression thecondition, EvaTCPLaexpression thethenpart, EvaTCPLelseif theelseifpart, EvaTCPLaexpression theelsepart)
    {
      super();
      condition = thecondition;
      thenpart = thethenpart;
      elseifpart = theelseifpart;
      elsepart = theelsepart;
    }

  /** Evaluate this arithmetic expression. */
  public double evaluate()
    {
      EvaTCPLelseif curelseif;

      if (condition.evaluate()) {
	return thenpart.evaluate();
      }
      else {
	// Check elseif parts
	curelseif = elseifpart;
	while (curelseif!=null) {
	  if (curelseif.condition.evaluate())
	    return curelseif.thenpart.evaluate();
	  else
	    curelseif = curelseif.next;
	}
	    
	// Finally return elsepart
	return elsepart.evaluate();
      }
    }

  public String toString()
    {
      StringBuffer res = new StringBuffer();
      res.append("if (");
      res.append(condition);
      res.append(") then ");
      res.append(thenpart);
      if (elseifpart!=null) {
	res.append(elseifpart);
      }
      res.append(" else ");
      res.append(elsepart);

      return res.toString();
    }

}
